home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
mac
/
Code
/
Chapter Code.exe
/
Chapter08
/
RotateMoveViewpointPanel.java
< prev
next >
Wrap
Text File
|
2000-07-03
|
3KB
|
128 lines
package applets;
import shout3d.*;
import shout3d.core.*;
import shout3d.math.*;
public class RotateMoveViewpointPanel extends Shout3DPanel implements DeviceObserver{
Viewpoint camera;
int pixelStartX;
int pixelStartY;
int pixelEndX;
int pixelEndY;
float[] worldPos = new float[3];
float [] eulers = new float [3];
float [] axisAngle = new float [4];
Quaternion q = new Quaternion();
public RotateMoveViewpointPanel (Shout3DApplet applet){
super(applet);
}
public void customInitialize() {
addDeviceObserver(this,"MouseInput", null);
camera = (Viewpoint) getCurrentBindableNode("Viewpoint");
worldPos = camera.position.getValue();
axisAngle = camera.orientation.getValue();
q.setAxisAngle(axisAngle);
q.getEulers(eulers);
}
protected void finalize() {
removeDeviceObserver(this,"MouseInput");
}
public boolean onDeviceInput(DeviceInput di, Object userData) {
MouseInput mi = (MouseInput) di;
switch (mi.which){
case MouseInput.DOWN:
pixelStartX = mi.x;
pixelStartY = mi.y;
return true;
case MouseInput.DRAG:
//PIXEL OPERATIONS
pixelEndX = mi.x;
pixelEndY = mi.y;
int dragDistanceX = pixelEndX - pixelStartX;
int dragDistanceY = pixelEndY - pixelStartY;
pixelStartX = pixelEndX;
pixelStartY = pixelEndY;
//ROTATING THE VIEWPOINT
//convert x drag to rotation
// at 30 pixels/radian
float rotationDelta = -(dragDistanceX/30f);
//compute new heading
eulers[0] = eulers[0] + rotationDelta;
//Convert from Eulers to AxisAngle
// using the Quaternion
q.setEulers(eulers);
q.getAxisAngle(axisAngle);
//set the Transform to
//updated axis angle values
camera.orientation.setValue(axisAngle);
//TRANSLATING THE VIEWPOINT
//covert y drag to distance
//at 2 meters per pixel
float translationDelta = dragDistanceY * 2.0f;
//make a vector pointing
//in z direction
float[] vector = {0,0, translationDelta};
//rotate the vector using
//value in quaternion
q.xform(vector);
//add rotated vector
//to current position
worldPos[0] = worldPos[0] + vector[0];
worldPos[2] = worldPos[2] + vector[2];
//set Transform to new position
camera.position.setValue(worldPos);
return true;
}//end of switch
return false;
}
} //end of class